package demo.niuke.demo1;


import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.PriorityQueue;
import java.util.Scanner;
import java.util.Set;

/**
 * @author : feixiang.li
 * @since : 2025-09-20 15:39
 */
public class Main {

    static int[] fa = new int[101];

    public static int find(int a) {
        if (fa[a] != a) {
            fa[a] = find(fa[a]);
            return fa[a];
        }
        return a;
    }

    public static void merge(int a, int b) {
        a = find(a);
        b = find(b);
        if (a == b) {
            return;
        }
        if (a > b) {
            fa[a] = b;
        } else {
            fa[b] = a;
        }
    }

    public static void main(String[] args) {

        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        int[][] matrix = new int[n][n];
        String s = sc.next();
        String[] split = s.split(",");
        List<Integer> list = new ArrayList<>();
        for (int i = 0; i < split.length; i++) {
            list.add(Integer.parseInt(split[i]));
        }
        for (int i = 0; i < n; i++) {
            s = sc.next();
            split = s.split(",");
            for (int j = 0; j < n; j++) {
                matrix[i][j] = Integer.parseInt(split[j]);
            }
        }
        if (list.size() <= 0) {
            System.out.println(0);
            return;
        }
        // 初始化father数组
        for (int i = 0; i < n; i++) {
            fa[i] = i;
        }

        // 遍历感染人员
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < n; j++) {
                if (matrix[i][j] == 1) {
                    merge(i, j);
                }
            }
        }

        Set<Integer> set = new HashSet<>();
        for (int i = 0; i < list.size(); i++) {
            set.add(find(list.get(i)));
        }

        Set<Integer> set1 = new HashSet<>();
        for (int i = 0; i < n; i++) {
            if (set.contains(find(i))) {
                set1.add(i);
            }
        }
        // 获取到被感染到组织
        System.out.println(set1.size()-list.size());
    }


}
